<?php

namespace app\api\controller;

use app\common\controller\Api;
use fast\Ecar;
use fast\Snowflake;
use think\Db;
use think\exception\PDOException;

/**
 * 课程接口
 */
class Course extends Api
{

    //protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //课程分类
    public function course_type()
    {


        $pid = input("pid");
        if (empty($pid)) {
            $pid = 0;
        }

        $list = Db::name("course_type")
            ->where("pid",$pid)
            ->where("status",1)
            ->select();

        $this->success('请求成功', $list);

    }


    //课程列表
    public function course_list()
    {

        $limit = input("limit");
        if (empty($limit)) {
            $limit = 10;
        }
        $page = input("page");
        if (empty($page)) {
            $page = 1;
        }

        $offset = ($page - 1) * $limit;
        $list = Db::name("course_info");

        $quick_search = input("quick_search");
        if (!empty($quick_search)) {
            $list = $list->where("name","like","%$quick_search%");

        }

        $type = input("course_type_id");
        if (!empty($type)) {
            $list = $list->where("course_type_id",$type);

        }

        $list = $list->limit("$offset,$limit")
            ->where("status",1)
            ->select();
        $this->success('请求成功', $list);

    }

    //创建课程 订单
    public function create_course_order()
    {
        $course_id = input("course_id");
        $course = Db::name("course_info")->where("id",$course_id)->find();
        if (empty($course)) {
            $this->error("课程不存在");
        }


        $snowflake = new Snowflake(time(), 1577836800000, 10, 12); // 10位节点ID，12位序列号，起始时间为2020-01-01 00:00:00
        $order_no = $snowflake->nextId(); // 生成订单号
        $data["order_no"] = "C".$order_no; //订单号
        $data["price"] = $course["price"]; //支付金额
        $data["pay_status"] = 0; //支付状态 0未付款 1已支付


        $data["name"] = $course["name"];
        $data["image"] = $course["image"];
        $data["introduce"] = $course["introduce"];
        $data["content"] = $course["content"];
        $data["files"] = $course["files"];
        $data["course_type_id"] = $course["course_type_id"];
        $data["course_id"] = $course["id"];


        $result = Db::name("course_order")
            ->where("user_id",$this->auth->id)
            ->find($data);
        if (!empty($result)) {
            if ($result["pay_status"] == 1) {
                $this->error("已经购买的课程无需重复购买");
            }
            $result = Db::name("course_order")->where("id",$result["id"])->update($data);

        } else {
            $data["user_id"] = $this->auth->id; //用户id
            $data["createtime"] = time();
            $result = Db::name("order")->insertGetId($data);
        }

        if ($result) {
            $this->success("下单成功",$order_no);
        } else {
            $this->error("下单失败");
        }

    }


    //课程列表
    public function my_course_list()
    {

        $limit = input("limit");
        if (empty($limit)) {
            $limit = 10;
        }
        $page = input("page");
        if (empty($page)) {
            $page = 1;
        }

        $offset = ($page - 1) * $limit;
        $list = Db::name("course_info");

        $quick_search = input("quick_search");
        if (!empty($quick_search)) {
            $list = $list->where("name","like","%$quick_search%");

        }

        $type = input("course_type_id");
        if (!empty($type)) {
            $list = $list->where("course_type_id",$type);

        }


        $list = $list->limit("$offset,$limit")
            ->where("user_id",$this->auth->id)
            ->select();
        $this->success('请求成功', $list);

    }


}
